package com.controller.ht;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.User;
import com.entity.UserIndustry;
import com.service.UserIndustryService;
import com.service.UserService;

@Controller
@RequestMapping("/userIndustry/")
public class userIndustryController extends BaseController<Object>{

	@Autowired
	private UserIndustryService uiservice;
	@Autowired
	private UserService userservice;
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public ModelAndView findAll(UserIndustry ui,String id,User user){
		ModelAndView mv =  new ModelAndView();
		user.setId(id);
		if(!user.getId().equals("0")){
			List<User> uiList = userservice.find(user);
			if(uiList.size()>0){
				user = uiList.get(0);
			}
		}
		ui.setId("0");
		List<UserIndustry> uis = uiservice.find(ui);
		mv.addObject("user", user);
		mv.addObject("uis",uis);
		mv.setViewName("pages/userInfo");
		return mv;
	}
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/findAllUI")
	@ResponseBody
	public AjaxRes findAllUI(UserIndustry ui){
		AjaxRes res = new AjaxRes();
		List<UserIndustry> uis = uiservice.find(ui);
		res.setSucceed(uis,"数据获取成功");
		return res;
	}
	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(UserIndustry ui,Page<UserIndustry> p){
		AjaxRes res = new AjaxRes();
		Page<UserIndustry> pageUis = uiservice.findByPage(ui, p);
		res.setSucceed(pageUis, "数据获取成功");
//		List<userIndustry> uis = pageUis.getResults();
//		mv.setViewName("pulicpage/index");
		return res;
	}
	/**
	 * 修改行业信息并返回修改结果
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public AjaxRes update(UserIndustry ui){
		AjaxRes res=new AjaxRes(); 
		if(ui.getId().equals("0")){
			ui.setId(UuidUtil.get32UUID());
			//用于测试是否添加成功的变量
			Integer a =uiservice.insert(ui);
			if(a==0){
				res.setResMsg("添加失败");
			}else{
				res.setResMsg("添加成功");
			}
		}else{
			//用于测试是否修改成功的变量
			Integer a =uiservice.update(ui);
			if(a==0){
				res.setResMsg("修改失败");
			}else{
				res.setResMsg("修改成功");
			}
		}
		return res;
	}
	@RequestMapping("/delect")
	@ResponseBody
	public AjaxRes delect(UserIndustry ui){
		AjaxRes res = new AjaxRes();
		//用于测试是否添加成功的变量
		Integer delRes = uiservice.delete(ui);
		if(delRes==0){
			res.setResMsg("删除失败");
		}else{
			res.setResMsg("删除成功");
		}
		return res;
	}

	//批量删除
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public AjaxRes deleteBatch(String uiCheck){
		AjaxRes res=new AjaxRes(); 
		List uiList=new ArrayList();
		String[] getArr = uiCheck.split(",");
		for(int i=0;i<getArr.length;i++){
			if(i!=0){
				UserIndustry ui = new UserIndustry();
				ui.setId(getArr[i]);
				uiList.add(ui);
			}
		}
		Integer a = uiservice.deleteBatch(uiList);
		if(a==0){
			res.setResMsg("批量删除失败");
		}else{
			res.setResMsg("批量删除成功");
		}
		return res;
	}
	@RequestMapping("/industryInfo")
	public String industryInfo(Model model,String id){
		UserIndustry ui = new UserIndustry();
		ui.setId(id);
		if(!id.equals("0")){
			List<UserIndustry> uiList = uiservice.find(ui);
			if(uiList.size()>0){
				ui = uiList.get(0);
			}
		}
		model.addAttribute("ui", ui);
		return "pages/industryInfo";
	}
}
